
(function(){
    common.FourLinkage(".province", ".city", ".country", ".plate");//创建搜索条件部分省市区域四级联动的下拉列表
    var checkedObject = {};
    /**
     * 小区列表  列表初始化设置
     * @type {Object}
     */
    var settings = {
        "sAjaxSource": window.globalHost + "/agent-sys/admin/villageInfo/index.do",
        "aoColumns": [{
            "mDataProp": function(obj) {
                var checkboxItemStr = '<input id="checkbox-' + obj.id + '" type="checkbox" >';
                if (checkedObject[obj.id]) {
                    checkboxItemStr = '<input id="checkbox-' + obj.id + '" type="checkbox" checked="checked">';
                }
                return '<div class="checkbox checkbox-danger" style="width:100%">' + checkboxItemStr + '<label for="checkbox-' + obj.id + '" style="width:80%"></label></div>'
            },
            "sTitle": "",
            "sDefaultContent": "",
            "sClass": "center"
        }, {
            "mDataProp": function (obj) {
                return obj['shengName']+'>'+obj['shiName']+'>'+obj['quName']+'>'+obj['blockName'];
            },
            "sTitle": "省市区板块",
            "sDefaultContent": ""
        }, {
            "mDataProp": function (obj) {
                return '<a href="javascript:;" class="villageName" data-id="'+obj['id']+'">'+obj['villageName']+'</a>'
            },
            "sTitle": "小区名称",
            "sDefaultContent": ""
        }, {
            "mDataProp": 'villageAddress',
            "sTitle": "小区地址",
            "sDefaultContent": ""
        }, {
            "mDataProp": "createTime",
            "sTitle": "创建时间",
            "sDefaultContent": ""
        }],
        "fnServerParams": function(aoData) {
            return common.getSearchData("form.villageSearch",aoData);
        }
    };
    var options = $.extend(true, {}, _dataTableSettings, settings);
    //获取列表table并渲染列表内的信息
    var $currentDataTable=$(".dataTables-village-list");
    var oTable = $currentDataTable.dataTable(options);
    /**
     * 绑定数据列表中的多选框
     * @param  {[type]} ev){                     var checkItem [description]
     * @return {[type]}       [description]
     */
    $currentDataTable.on("change", ".checkbox", function(ev) {
        var checkItem = $(this).find("input[type='checkbox']")[0];
        var checkId = checkItem.id.replace('checkbox-', '');
        if (checkItem.checked) {
            checkedObject[checkId] = true;
        } else {
            delete checkedObject[checkId];
        }
        ev.stopPropagation();
    });
    /**
     * 绑定搜素按钮的点击事件、触发搜索动作。
     * @param  {[type]} ) {                   oTable.fnClearTable();        return false;    } [description]
     * @return {[type]}   [description]
     */
    $(".searchButton").click(function() {
        oTable.fnClearTable();
        return false;
    });
    /**
     * 绑定重置表单按钮的点击事件、触发重置表单控件。
     */
    $(".resetForm").click(function () {
        $('.result-selected').removeClass('result-selected');
        $('.chosen-single').children('span').html('请选择...');
    });
    //点击列表内的小区名称则弹框出现小区详情页面
    $currentDataTable.off("click").on("click",".villageName",function (ev) {
        //更新小区信息
        var self=$(this);
        var selfId=self.attr("data-id");
        common.openInsertDialog({
            'url': './basicInfo/villageInfo.html?villageId='+selfId,
            'title': '小区详情',
            'sAjaxSource': window.globalHost + '/agent-sys/admin/villageInfo/update.do',
            'area': ['1000px', '600px'],
            'btn': '保存',
            ok: function () {
                oTable.fnClearTable();
            }
        });
        ev.stopPropagation();
    });
    //点击删除小区按钮则触发搜索所有列表内被勾选的小区信息，并将要删除的小区id传送给后台
    $("button.deleteVillageButton").on("click", function(ev) {
        common.deleteConfirm(batchDelete);
        ev.stopPropagation();
    });
    function batchDelete() {
        var ids = [];
        if ($.isEmptyObject(checkedObject)) {
            layer.alert("请选择需要删除的小区 !");
            return;
        }
        $.each(checkedObject, function(i, I) {
            ids.push(i);
        });
        ids = ids.join();
        $.post(window.globalHost + '/agent-sys/admin/villageInfo/batchDelete.do', { ids: ids }).success(function(data) {
            var record = common.transformData(data);
            layer.alert(record.res.msg);
            /**
             * 删除成功之后，清除本地的checkbox缓存。
             * @type {Object}
             */
            checkedObject = {};
            /**
             * 批量删除成功之后，刷新列表
             */
            oTable.fnClearTable();
        });
    }
    //点击添加小区则弹框出现 添加小区页面
    $("button.insertHouseButton").on('click', function (ev) {
        common.openInsertDialog({
            'url': './basicInfo/insertVillage.html',
            'title': '添加小区',
            'sAjaxSource': window.globalHost + '/agent-sys/admin/villageInfo/save.do',
            'area': ['1000px', '600px'],
            'btn': '保存',
            ok: function () {
                oTable.fnClearTable();
            }
        });
        ev.stopPropagation();
    });

})();
